// import axios from 'axios';
import deploy from '../config/deploy';
// import $ from 'jquery'
const root = window.root;
const util = {
  interval(fn, time = 60000){
    return setInterval(() => {
      fn();
    }, time)
  },
  uuid () {
    let i, random;
    let uuid = '';
    for (i = 0; i < 32; i++) {
      random = Math.random() * 16 | 0;
      if (i === 8 || i === 12 || i === 16 || i === 20) {
        uuid += '-';
      }
      uuid += (i === 12 ? 4 : (i === 16 ? (random & 3 | 8) : random))
          .toString(16);
    }
    return uuid;
  },
  getChartColors () {
    return ['#5bd4c7', '#62a9ed', '#8bc34a', '#fabb3d', '#c090ec', '#67c2ef', '#fcdd5f', '#fd7979', '#7381ce'];
  },
  /**
   * 保留n位小数
   * @param srcNumber
   * @param n
   * @param isPad
   * @returns {*}
   */
  toDecimal(srcNumber, n = 3, isPad) {
    let dstNumber = parseFloat(srcNumber);
    if (isNaN(dstNumber)) {
      return srcNumber;
    }
    if (dstNumber >= 0) {
      dstNumber = parseInt(dstNumber * Math.pow(10, n) + 0.5) / Math.pow(10, n);
    } else {
      let tmpDstNumber = -dstNumber;
      dstNumber = parseInt(tmpDstNumber * Math.pow(10, n) + 0.5) / Math.pow(10, n);
    }
    let dstStrNumber = dstNumber.toString();
    let dotIndex = dstStrNumber.indexOf('.');
    //是否补0
    if (isPad) {
      if (dotIndex < 0) {
        dotIndex = dstStrNumber.length;
        dstStrNumber += '.';
      }
      while (dstStrNumber.length <= dotIndex + n) {
        dstStrNumber += '0';
      }
    }

    return dstStrNumber;
  },
  /**
   * 补零
   * @param num
   * @returns {string}
   */
  pad (num) {
    return new Array(2 - ('' + num).length + 1).join(0) + num;
  },
  /**
   * 格式化日期
   * @param val 时间戳（例如：1477363297）
   * @param type 1：完整显示，2：不显示年,3:不显示分秒
   */
  formatDate (val, type = 1) {
    let result = '';
    let timestamp = val;
    let d = new Date();
    d.setTime(timestamp * 1000);
    let year = d.getFullYear();
    let month = this.pad(d.getMonth() + 1);
    let day = this.pad(d.getDate());
    let housrs = this.pad(d.getHours());
    let minutes = this.pad(d.getMinutes());
    let seconds = this.pad(d.getSeconds());
    switch (type) {
      case 1:
        result = year + '-' + month + '-' + day + ' ' + housrs + ':' + minutes + ':' + seconds;
        break;
      case 2:
        result = month + '-' + day + ' ' + housrs + ':' + minutes;
        break;
      case 3:
        result = year + '-' + month + '-' + day;
        break;
      case 4:
        result = minutes + ':' + seconds;
        break;
      case 5:
        result = year + '-' + month + '-' + day + ' ' + housrs + ':' + minutes;
        break;
      default:
        result = year + '-' + month + '-' + day + ' ' + housrs + ':' + minutes + ':' + seconds;
        break
    }
    return result;
  },
  //jquery方式的ajax请求
  /*
   api: function (opts, customOpts) {
   let EMPTY_FN = function () {
   };
   let that = this;
   let success = opts.success || EMPTY_FN;
   let beforeSend = opts.beforeSend || EMPTY_FN;
   let error = opts.error || EMPTY_FN;
   let complete = opts.complete || EMPTY_FN;
   if (!opts.url) {
   opts.url = ''
   }
   if (opts.surl) {
   opts.url = opts.surl;
   } else {
   opts.url = root.API_PATH + opts.url;
   }

   opts.data = $.extend({
   // "ver": root.versions,
   // "user_id": USER_ID,
   // "org_id": ORG_ID,
   // "firsthand": true,
   // "user_token": TOKEN
   }, opts.data);

   opts = $.extend({
   timeout: 1000 * 30,
   type: 'get',
   autoJumpLogin: true,
   'processData': false,
   contentType: 'application/json',
   dataType: 'json'
   }, opts, {
   success: function (responseData, textStatus, jqXHR) {
   let status = responseData.status;
   if (status == 401 && opts.autoJumpLogin) {

   }
   return success.apply(this, arguments);
   },
   beforeSend: function () {
   return beforeSend.apply(this, arguments);
   },
   error: function (jqXHR, textStatus) {
   //that.showErrorInfo(jqXHR);
   return error.apply(this, arguments);
   },
   complete: function (jqXHR, textStatus) {
   return complete.apply(this, arguments);
   }
   });
   //post数据使用json字符串格式
   if (opts.type.toLowerCase() == "post" && opts.data) {
   opts.data = JSON.stringify(opts.data);
   }

   return $.ajax(opts);

   },
   */
  //axios方式的ajax请求
  /*
   ajax(option){
   let instance = {};
   if (option.ourl) {
   option.url = option.ourl;
   } else {
   option.url = deploy.openOrigin ? option.url : deploy.apiHost + deploy.apiPrifix + option.url;
   }
   option.baseURL = deploy.baseURL;
   option.params = Object.assign({
   ver: deploy.versions,
   }, option.params);
   option.data = Object.assign({}, option.data);
   axios.interceptors.request.use(function (config) {
   return option.beforeSend ? option.beforeSend(config) || config : config;
   }, function (error) {
   return Promise.reject(error);
   });
   axios.interceptors.response.use(function (response) {
   return option.affterSend ? option.affterSend(response) || response : response;
   }, function (error) {
   return Promise.reject(error);
   });
   instance = axios(Object.assign({
   transformRequest: [function (data) {
   if (deploy.postData === 'object') {
   return data
   } else {
   return JSON.stringify(data)
   }
   }],
   timeout: deploy.timeout,
   responseType: 'json',
   xsrfCookieName: 'XSRF-TOKEN',
   withCredentials: false,
   headers: {'X-Requested-With': 'XMLHttpRequest'},
   transformResponse: [function (data) {
   return data;
   }],
   validateStatus: function (status) {
   return status >= 200 && status < 300;
   },
   maxContentLength: 2000,
   onUploadProgress: function (progressEvent) {
   },
   }, option));
   instance.then(function (response) {
   option.success && option.success(response.data);
   });
   instance.catch(function (error) {
   option.err && option.err(error);
   });
   return instance;
   }
   */
};
export default util;
